<template>
  <a-card :bordered="false" class="card-area">
    <a-form layout="inline">
      <a-form-item label="用户名" :labelCol="{span: 4}" :wrapperCol="{span: 18, offset: 2}">
        <a-input placeholder="请输入用户名" @pressEnter="fetch" v-model="searchName"/>
      </a-form-item>
    </a-form>
    <div>
      <div class="operator">
        <a-button type="primary" ghost @click="showAdd=true">新增</a-button>
        <a-button @click="batchDelete" v-hasPermission="'user:delete'">删除</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 900 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
      </a-table>
    </div>
    <a-modal
        title="添加成员"
        v-model="showAdd"
        width="30%"
        @cancel="showAdd=false" @ok="handleAddMember">
        <a-select
          mode="multiple"
          style="width: 100%;"
          v-model="added"
          placeholder="选择您要添加的成员">
        <a-select-option v-for="(item, i) in memberList" :key="i" :value="item.username">{{item.realname}}</a-select-option>
        </a-select>
    </a-modal>
  </a-card>
</template>

<script>
import DeptInputTree from '../system/dept/DeptInputTree'
import RangeDate from '@/components/datetime/RangeDate'

export default {
  name: 'Member',
  components: {DeptInputTree, RangeDate},
  props: ['projectInfoData'],
  data () {
    return {
      added: [],
      showAdd: false,
      memberList: [],
      selectedArr: [],
      searchName: '',
      paginationInfo: null,
      dataSource: [],
      selectedRowKeys: [],
      loading: false,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  computed: {
    columns () {
      return [{
        title: '用户名',
        dataIndex: 'memberUser'
      }, {
        title: '姓名',
        dataIndex: 'realname'
      }, {
        title: '性别',
        dataIndex: 'ssex',
        customRender: (text) => {
          switch (text) {
            case '0':
              return '男'
            case '1':
              return '女'
            case '2':
              return '保密'
            default:
              return text
          }
        }
      }, {
        title: '邮箱',
        dataIndex: 'email'
      }, {
        title: '部门',
        dataIndex: 'deptName'
      }, {
        title: '电话',
        dataIndex: 'mobile'
      }, {
        title: '状态',
        dataIndex: 'status',
        customRender: (text) => {
          switch (text) {
            case 0:
              return '正常'
            case 1:
              return '失效'
            default:
              return text
          }
        }
      }, {
        title: '创建时间',
        dataIndex: 'createTime'
      }]
    }
  },
  created () {
    this.fetch()
    this.getMemberList()
  },
  methods: {
    handleAddMember () {
      const params = {
        projectId: this.projectInfoData.projectId,
        memberUsers: this.added
      }
      this.$post('member/addProjectMembers', params, true).then(res => {
        if (res.data.data > 0) {
          this.$message.success('添加成员成功')
          this.fetch()
          this.showAdd = false
        }
      })
    },
    getMemberList () {
      const params = {
        projectId: this.projectInfoData.projectId
      }
      this.$post('member/userListNotInProject', params, true).then(res => {
        this.memberList = res.data.data
      })
    },
    onSelectChange (selectedRowKeys, val) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedArr = val
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的记录')
        return
      }
      this.$confirm({
        title: '确定删除所选中的记录?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          const params = {
            projectId: this.projectInfoData.projectId,
            memberUsers: this.selectedArr.map(v => v.memberUser)
          }
          this.$post('member/deleteProjectMembers', params, true).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedArr = []
            this.fetch()
          })
        },
        onCancel: () => {
          this.selectedRowKeys = []
          this.selectedArr = []
        }
      })
    },
    handleTableChange (pagination) {
      this.paginationInfo = pagination
      this.fetch()
    },
    fetch (params = {}) {
      // 显示loading
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        const { current, pageSize } = this.paginationInfo
        this.$refs.TableInfo.pagination.current = current
        this.$refs.TableInfo.pagination.pageSize = pageSize
        params.pageSize = pageSize
        params.pageNum = current
      } else {
        // 如果分页信息为空，则设置为默认值
        const { defaultCurrent, defaultPageSize } = this.pagination
        params.pageSize = defaultPageSize
        params.pageNum = defaultCurrent
      }
      params.projectId = this.projectInfoData.projectId
      params.memberUser = this.searchName
      this.$post('member/list', {
        ...params
      }, true).then((r) => {
        let data = r.data.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
@import "../../../static/less/Common";
.operator {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ant-input {
  width: 260px;
}
</style>
